import MySQLdb
import sys
import time

# usage example: $python importer/firewall.py pwd sample_ori_firewall sample/Firewall_Log.txt

DEBUG = 2

if __name__ == '__main__':
    db = MySQLdb.connect(host="rome.cs.vt.edu", port=5801, user="fusetester", passwd=sys.argv[1], db="fusebox")

    tablename = sys.argv[2]
    filename = sys.argv[3]
    hfile = open(filename)

    counter = 1
    for line in hfile.readlines():
        items = line.split()

        if DEBUG >= 3:
            print '#### ITEM ####'
            print items

        unixdate = int(time.mktime(time.strptime(':'.join(items[1:5]), "%b:%d:%Y:%H:%M:%S")))
        discription = ' '.join(items[10:])
        if len(discription) > 1024:
            print 'Warning: discription is out of space!'

        db.query('INSERT INTO %s (severity, date, syslogid, sourceip, sourceport, destip, destport, description) VALUES (%s, %d, %s, "%s", %s, "%s", %s, "%s")' % (tablename, items[0], unixdate, items[5], items[6], items[7], items[8], items[9], discription))

        if DEBUG >= 2:
            print '#### END %d ####' % (counter)
        counter += 1

    db.close()

